package com.cimr.service.manager.impl;

import com.cimr.comm.base.BaseDao;
import com.cimr.comm.base.BaseServiceImpl;
import com.cimr.comm.util.IdUtil;
import com.cimr.dao.manager.AreaDao;
import com.cimr.po.manager.Area;
import com.cimr.service.manager.AreaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: SysAreaServiceImpl</p>
 * <p>Description: 地区数据Service接口实现</p>
 */
@Service("mgrAreaService")
@Transactional
public class AreaServiceImpl extends BaseServiceImpl<Area> implements AreaService {

    @Resource
    private AreaDao areaDao;

    @Override
    protected BaseDao getDao() {
        return areaDao;
    }


    public List<Area> getList(String pid, String areaId, Long countryCode, String title, String code) {
        Area area = new Area();
        area.setPid(pid);
        area.setAreaId(areaId);
        area.setCountryCode(countryCode);
        area.setTitle(title);
        area.setCode(code);
        return areaDao.selectList_commom(area);
    }

    public Integer getListCount(String pid, String areaId, Long countryCode, String title, String code) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("areaId", areaId);
        map.put("pid", pid);
        map.put("countryCode", countryCode);
        map.put("title", title);
        map.put("code", code);
        return areaDao.findListCount(map);
    }

    @Override
    public void insert(String pid, String areaId, String title,
                       String code, Long countryCode, Float orderId) {

        Area entity = new Area();
        entity.setId(IdUtil.getId().toString());
        if (pid == null || "".equals(pid)) {
            pid = "1";
        }
        Area parent = this.getAreaByAreaId(pid, countryCode);
        if (parent != null) {
            entity.setpTitle(parent.getTitle());
        }
        entity.setPid(pid);
        entity.setAreaId(areaId);
        entity.setTitle(title);
        entity.setCode(code);
        entity.setCountryCode(countryCode);
        entity.setOrderId(orderId);
        entity.setBid(null);

        // 父级对象存在check

        // 1.添加根节点的情况：直接添加
        // 地区父级id pid = 1

        // 2.添加非根节点的情况：check
        if (pid != null && !"".equals(pid) && !"1".equals(pid)) {

            int cnt = this.getListCount(null, pid, null, null, null);

            if (cnt == 0) {
                return;
            }
        }

        // 3. 添加地区已存在
        int cnt = this.getListCount(null, areaId, null, null, null);
        if (cnt != 0) {
            return;
        }

        // 添加数据
        areaDao.insertObject_common(entity);

    }

    @Override
    public void update(String id, String pid, String areaId, String title,
                       String code, Long countryCode, Float orderId) {

        // 1. 更新对象存在check

        Area sa = this.selectById_common(id);
        if (sa == null) {
            return;
        }
        // 2.更新非根节点的情况：check
        if (pid != null && !"".equals(pid) && !"1".equals(pid)) {

            int cnt = this.getListCount(null, pid, null, null, null);

            if (cnt == 0) {
                // 是子节点&父节点不存在
                return;
            }
        }

        Area entity = new Area();
        entity.setId(id);
        if (pid == null || "".equals(pid)) {
            pid = "1";
        }
        Area parent = this.getAreaByAreaId(pid, countryCode);
        if (parent != null) {
            entity.setpTitle(parent.getTitle());
        }
        // 更新字段
        entity.setPid(pid);
        entity.setAreaId(areaId);
        entity.setTitle(title);
        entity.setCode(code);
        entity.setCountryCode(countryCode);
        entity.setOrderId(orderId);
        // 更新数据
        areaDao.updateObject_common(entity);

    }

    public Area getAreaByAreaId(String areaId, Long countryCode) {
        Area area = null;
        List<Area> list = this.getList(null, areaId, countryCode, null, null);
        if (list.size() > 0) {
            area = list.get(0);
        }
        return area;
    }

}
